package vikaa.adapter;


import java.util.ArrayList;
import java.util.Vector;

import vikaa.wowo.R;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter{    
    private Context mContext;			// 定义Context
    private Vector<Integer> mImageIds = new Vector<Integer>();	// 定义一个向量作为图片源
    private Vector<Boolean> mImage_bs = new Vector<Boolean>();	// 定义一个向量作为选中与否容器
    
    private int lastPosition = -1;		//记录上一次选中的图片位置，-1表示未选中任何图片
    private boolean multiChoose;		//表示当前适配器是否允许多选
    private boolean first;
    private final ArrayList<String> mlist;
    
    public ImageAdapter(Context c, boolean isMulti, ArrayList<String> list){
    	mContext = c;
    	multiChoose = isMulti;
    	mlist = list;
    	first = true;
    	for(int i=0; i<98; i++)
    		mImage_bs.add(false);
    }
    
	public int getCount() {
		// TODO Auto-generated method stub
		return mlist.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		 return position;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		if(first){
			for(int i=0; i<mlist.size(); i++)
	    		mImage_bs.add(false);
				first =false;
		}
		// TODO Auto-generated method stub
		convertView = LayoutInflater.from(mContext).inflate(R.layout.photolist_item, null);
		imgHolder ih = new imgHolder();
		ih.photolist_item = (ImageView)convertView.findViewById(R.id.photolist_item);
		convertView.setTag(mlist.get(position).toString());
		ih.photolist_item.setImageDrawable(makeBmp(mlist.get(position),mImage_bs.elementAt(position)));
        return convertView;
	}
	class imgHolder {
    	public ImageView photolist_item = null;
    }
	private LayerDrawable makeBmp(String id, boolean isChosen){
		//Bitmap mainBmp = ((BitmapDrawable)mContext.getResources().getDrawable(id)).getBitmap();
		Bitmap mainBmp = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(id), 100, 100, true);  
		// 根据isChosen来选取对勾的图片
    	Bitmap seletedBmp;
    	if(isChosen == true)
    		{
    		seletedBmp = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.photo_selected);}
    	else
    		seletedBmp = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.photo_unselected);
    	
    	// 产生叠加图
    	Drawable[] array = new Drawable[2];
    	array[0] = new BitmapDrawable(mainBmp);
    	array[1] = new BitmapDrawable(seletedBmp);
    	LayerDrawable la = new LayerDrawable(array);
    	la.setLayerInset(0, 0, 0, 0, 0);
    	la.setLayerInset(1, 0, 0, 200, 200 );
    	 
    	return la;	//返回叠加后的图
    }

	// 修改选中的状态
    public void changeState(int position){
    	// 多选时
    	if(multiChoose == true){	
    		mImage_bs.setElementAt(!mImage_bs.elementAt(position), position);	//直接取反即可	
    	}
    	notifyDataSetChanged();		//通知适配器进行更新
    }
}
